window.onload = function() {
  let problems = JSON.parse(localStorage.getItem("tempproblems"))
  console.log(problems)
  if (problems == null) {
    problems = new Array();
    localStorage.setItem("tempproblems", JSON.stringify(problems))
    
  } else {
    for (let index=0; index < problems.length; index++) {
      const elem = problems[index];
      let ty = "";
      ty += elem.problem
      if(elem.problemType == "1") {
        ty += "(单选题)";
      } else if (elem.problemType == "2") {
        ty += "(多选题)";
      } else {
        ty += "(文本题)";
      }

      let xuan = "";
      let sunxu = 'A';
      if (elem.problemType != "3") {
        for (let index2=0; index2 < elem.detail.length; index2++) {
          xuan += "<div><strong>"+String.fromCharCode(thechar.charCodeAt(0) + index2)+ "</strong>&nbsp;&nbsp;&nbsp;" + elem.detail[index2] +"</div>";
        }
      } else {
        xuan += "<br><br><br>"
      }
      
      let ht = "\
      <div class=\"question_box\">\
          <div>QU</div>\
          <div class=\"question_manner\">\
            <h3>"+ty+"</h3>"+xuan+"\
          </div>\
          <div class=\"handles\">\
                <div class=\"handle\"><button onclick=\"up(this)\">上移</button></div>\
                <div class=\"handle\"><button onclick=\"down(this)\">下移</button></div>\
                <div class=\"handle\"><button onclick=\"cp(this)\">复用</button></div>\
                <div class=\"handle\"><button onclick=\"rm(this)\">删除</button></div>\
              </div>\
            </di\
            v>";

      document.getElementById("putpro").innerHTML += ht
          
     }
       
  }
  
}

// 弹窗选项添加
// 添加单选选项
let thechar = 'A';
function option_add(){
  let newChar = String.fromCharCode(thechar.charCodeAt(0) + 1);
  let input = document.getElementById('add_title')
  let add = document.createElement("input")
  add.setAttribute("id", newChar+"1")
  let br = document.createElement('br')  
  let su = document.createTextNode(newChar)
  document.getElementById('new1').appendChild(su)
  document.getElementById('new1').appendChild(add)
  document.getElementById('new1').appendChild(br)
  thechar = newChar;
}
// 添加多选选项
function option_add2(){
  let newChar = String.fromCharCode(thechar.charCodeAt(0) + 1);
  let input = document.getElementById('add_title')
  let add = document.createElement("input")
  add.setAttribute("id", newChar+"2")
  let br = document.createElement('br')  
  let su = document.createTextNode(newChar)
  document.getElementById('new2').appendChild(su)
  document.getElementById('new2').appendChild(add)
  document.getElementById('new2').appendChild(br)
  thechar = newChar;
}

// 单选添加按钮
function saveTime1() {
  let t = 'A'
  let json = {};
  json.problemType = "1";
  json.problem = document.getElementById("title1").value;
  let det = new Array();
  thechar = String.fromCharCode(thechar.charCodeAt(0) + 1);
  while(t != thechar) {
    let text = document.getElementById(t+"1").value;

    det.push(text)

    t = String.fromCharCode(t.charCodeAt(0) + 1)
  }
  json.detail = det
  let problems = JSON.parse(localStorage.getItem("tempproblems"))
  problems.push(json);
  localStorage.setItem("tempproblems", JSON.stringify(problems))
  location.href = "questionEdit.html"
}

// 多选添加按钮
function saveTime2() {
  let t = 'A'
  let json = {};
  json.problemType = "2";
  json.problem = document.getElementById("title2").value;
  let det = new Array();
  thechar = String.fromCharCode(thechar.charCodeAt(0) + 1);
  while(t != thechar) {
    let text = document.getElementById(t+"2").value;

    det.push(text)

    t = String.fromCharCode(t.charCodeAt(0) + 1)
  }
  json.detail = det
  let problems = JSON.parse(localStorage.getItem("tempproblems"))
  problems.push(json);
  localStorage.setItem("tempproblems", JSON.stringify(problems))
  location.href = "questionEdit.html"
}

// 文本添加按钮
function saveTime3() {
  let json = {};
  json.problemType = "3";
  json.problem = document.getElementById("title3").value;

  let problems = JSON.parse(localStorage.getItem("tempproblems"))
  problems.push(json);
  localStorage.setItem("tempproblems", JSON.stringify(problems))
  location.href = "questionEdit.html"
}

// 日期组件
layui.use('laydate', function(){
  var laydate = layui.laydate;
  
  //执行一个laydate实例
  laydate.render({
    elem: '#endtime'
    ,type: 'datetime',
    min: 0,
    max: 30
  });
  
});

// 保存/发布 问卷
function saveQuestion(state) {
  let question = {}
  let questionUUID = generateUUID();
  let questionName = document.getElementById("questionName").value;
  question.questionTitle = questionName;
  question.state = String(state)
  let endtime = document.getElementById("endtime").value;
  question.endTime = endtime;
  question.problems = localStorage.getItem("tempproblems")

  let data = JSON.parse(localStorage.getItem("questionData")) 
  if (data == null) {
    data = {};
  } 
  data[questionUUID] = question
  localStorage.setItem("questionData", JSON.stringify(data))

  localStorage.removeItem("tempproblems")
  location.href = "index.html"
}


// 问卷题目操作
function up(isthis){
  // console.log(isthis)
  // console.log($(isthis).parents(".question_box"));
  let A = $(isthis).parents(".question_box");
  // console.log(A)
  let B = A.prev();
  // console.log(B)
  $(A).after(B);
}
function down(isthis){
  console.log(isthis)
  console.log($(isthis).parents(".question_box"));
  let A = $(isthis).parents(".question_box");
  console.log(A)
  let B = A.next();
  console.log(B)
  $(B).after(A);
}
function cp(isthis){
  $(isthis).parents(".question_box").after($(isthis).parents(".question_box").clone(true));
}
function rm(isthis){
  $(isthis).parents(".question_box").remove();
}